package com.dmall.graph.dfs.app;

import com.dmall.graph.Graph;

public class CC {

    private Graph G;
    private boolean[] visited;
    private int cccount = 0;

    public CC(Graph G) {
        this.G = G;
        this.visited = new boolean[G.V()];

        for (int i = 0; i < G.V(); i++) {
            if (!visited[i]) {
                dfs(i);
                cccount ++;
            }
        }
    }

    private void dfs(int v) {
        visited[v] = true;
        for (Integer w : G.adj(v)) {
            if (!visited[w]) {
                dfs(w);
            }
        }
    }

    public int count() {
        return cccount;
    }

    public static void main(String[] args) {
        String filePath = CC.class.getClassLoader().getResource("g.txt").getPath();
        Graph graph = new Graph(filePath);
        CC cc = new CC(graph);
        System.out.println(cc.count());
    }
}
